import { deleteCompany, getChannelList, getCompanyProductInfo, updateSaasCompany } from '@/api/company'
import { Component, Emit, Prop, Watch } from 'vue-facing-decorator'
import Render from '@/render/Render'
import renderModal, {
    getModelValue,
    initModelValue,
    type FormModalDto
} from '@/components/modal/Modal'
import toNative from '@/utils/toNative'
import { getReceiptData } from '@/utils/util'
import type { CompanyDetailDto, CompanyInfoDto, CompanyProductInfoDto } from '@/api/company/types'
import { message } from 'ant-design-vue'
import { closeProduct } from '@/api/product'

@Component
export class DialogRemoveCompany extends Render {
    @Prop({ default: false }) show!: boolean
    @Prop() companyId!: number
    @Prop() companyName!: string

    @Emit('update:show')
    showChange(val: boolean) {
        return val
    }

    @Emit('refresh')
    handleRefresh() {}

    @Watch('show', { immediate: true })
    async handleWatchShow() {
        this.formConfig.show = this.show
    }

    formConfig = this.initFormConfig() as FormModalDto

    initFormConfig() {
        return {
            title: '删除客户账号',
            width: '450px',
            theme: 'dark',
            show: false,
            items: {
                companyName: {
                    name: '',
                    element: 'input',
                    placeholder: '请输入客户名称验证 ',
                    required: true
                },
            },
            complete: async () => {
                const data = getModelValue(this.formConfig)
                if (data.companyName !== this.companyName) {
                    message.error('客户名称输入错误')
                    return
                }
                
                await deleteCompany({
                    companyId: this.companyId
                })

                message.success('操作成功')
                this.handleRefresh()
                this.formConfig.show = false
                this.showChange(false)
            },
            cancel: () => {
                this.formConfig.show = false
                this.showChange(false)
            }
        } as FormModalDto
    }

    async getData() {
        const res  = await getCompanyProductInfo({
            companyId: this.companyId
        })
        return res
    }

    render() {
        return <div>{renderModal(this.formConfig)}</div>
    }
}

export default toNative<{ companyId: number, companyName: string }, { refresh: () => void }>(DialogRemoveCompany)
